<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <title>更改密码</title>
    <script src="js/jquery-3.7.0.min.js"></script>
    <script src="js/bootstrap.js"></script>
    <script src="js/vue.min.js"></script>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
    <link href="https://getbootstrap.com/docs/5.3/assets/css/docs.css" rel="stylesheet">
    <link rel="stylesheet" href="css/bootstrap.css">
</head>
<body>
<div id="app" class="main">
    <div class="form-group">
        <label for="password">新密码:</label>
        <input type="password" v-model="password" id="password" class="form-control" placeholder="请输入新密码">
    </div>
    <div class="form-group">
        <label for="confirmPassword">确认密码:</label>
        <input type="password" v-model="confirmPassword" id="confirmPassword" class="form-control" placeholder="请再次输入新密码">
    </div>
    <button @click="submitPassword" class="btn btn-primary">提交</button>
</div>

<script>
    new Vue({
        el: '#app',
        data: {
            password: '', // 确保 password 在 data 中定义
            confirmPassword: ''
        },
        methods: {
            submitPassword() {
                // 检查密码是否匹配
                if (this.password !== this.confirmPassword) {
                    alert('密码和确认密码不匹配');
                    return;
                }

                // 如果密码为空
                if (!this.password) {
                    alert('密码不能为空');
                    return;
                }

                // 构建请求数据
                const requestData = {
                    password: this.password
                };
                let password=this.password;
                // 发送 AJAX 请求
                $.ajax({
                    url: "updateStudentPassword/"+password, // 确保这是正确的API URL
                    type: "post",
                    contentType: "application/json", // 添加此行以确保发送JSON数据
                    data: JSON.stringify(requestData), // 使用 JSON 字符串化数据
                    success: function (response) {
                        console.log('Server response:', response); // 输出服务器响应内容
                        alert('密码更改成功');
                    },
                    error: function (xhr, status, error) {
                        console.error('Error changing password:', xhr.responseText || error);
                        console.log('Status:', status);
                        console.log('Full XHR object:', xhr); // 输出完整的XHR对象
                        alert('密码更改失败，请重试');
                    }
                });
            }
        }
    });
</script>
</body>
</html>